import {getStorageSync} from "./Storage";
module.exports = {

    /*
    *时间戳转换成日期函数
    * 待优化
    * */
    getDate: function(t = "") { //t是要转的13位时间戳
        let date = new Date();
        if (t) {
            let str = typeof (t) !== "number" && t.indexOf ("-") !== -1 ? t.replace(/-/g, "/") : t;
            date = new Date(str);
        }
        let Y = date.getFullYear() + "-",
            M = (date.getMonth()+1 < 10 ? "0"+(date.getMonth()+1) : date.getMonth()+1) + "-",
            D = parseInt(date.getDate()) < 10 ? ("0" + date.getDate() + " ") : date.getDate() + " ",
            h = date.getHours() < 10 ? ("0" + date.getHours() + ":") : date.getHours() + ":",
            m = date.getMinutes() < 10 ? ("0" + date.getMinutes() + ":") : date.getMinutes() + ":",
            s = date.getSeconds() < 10 ? ("0" + date.getSeconds()) : date.getSeconds();
        return Y + M + D + h + m + s;
    },

    /*
   * 验证手机号
   * */
    verifyPhone: function (tel) {
        return (/^[1][3,4,5,6,7,8,9][0-9]{9}$/.test(tel))
    },

    /*
    * 验证信息
    * 字符串不为空 且 不为空格
    * 如果为空或者空白 返回fasle
    * */
    verifyInput: function (str) {
        let reg = "^[ ]+$";
        let re = new RegExp(reg);
        if (str === "" || typeof(str) === "undefined") {
            return false
        } else if (re.test(str)){
            return false
        }
        return true
    },

    /*
    * 将对象参数拼接字符串(放入url之后的参数)
    * */
    optionsToString: function (params = {}) {
        if (Object.keys(params).length > 0) {
            let str = "";
            for (let i in params) {
                str += (i + "=" + params[i] + "&")
            }
            str = str.substr(0, str.length - 1);
            return "?" + str
        } else {
            return ""
        }
    },

    /*
     * 返回wx.navigateBack 层级
     * 根据pageUrl页面路径参数 查找pages栈中存在相同路径的层级数
     * 如果传入的参数为空  则返回首页
     * 如果page栈中存在传入的路径参数 则返回该页面在page中所在的位置
     * 如果栈中没有找到匹配路径  跳转到该路径页面
     * tabsPage tab页面集合
     * */
    navigatePage: function (url, isRedirectTo) {
        const pages = getCurrentPages();
        const tabsPage = [
            "/pages/home/index/index",
            "/pages/news/index/index",
            "/pages/product/index/index",
            "/pages/user/index/index"
        ]
        let currentRouteInx = pages.length - 1;
        let currentIndex = "";
        let pageUrl = url && url !== "undefined" ? (url.substring(0, 1) === "/" ? url : ("/" + url)) : "";
        if (pageUrl) {
            for (let i = 0; i < pages.length; i ++) {
                if (pageUrl.split("?")[0] === ("/" + pages[i].route)) {
                    currentIndex = currentRouteInx - i;
                    break
                }
            }
            let isTab = tabsPage.some(function (item) {
                return item === pageUrl.split("?")[0]
            })
            setTimeout(function () {
                if (currentIndex) {
                    wx.navigateBack({delta: currentIndex})
                } else if (isTab) {
                    wx.switchTab({url: pageUrl})
                } else if (!!isRedirectTo) {
                    wx.redirectTo({url: pageUrl})
                } else {
                    wx.navigateTo({url: pageUrl})
                }
            }, 300);
        } else if (pages.length > 1) {
            setTimeout(function () {wx.navigateBack()}, 300);
        } else {
            wx.switchTab({url: tabsPage[0]}) //首页
        }
    },

    /*
    * 类型判断
    * */
    isType: function (value, type) {
        return Object.prototype.toString.call(value) === "[object " + type + "]"
    },

    /*
    * 影藏手机号
    * */
    hiddenPhoneNumber (tel){
        let reg = /^(\d{3})\d{4}(\d{4})$/;
        return tel.replace(reg, "$1****$2");
    },

    /*
    * 减法精准性
    * isPercent是否是百分数
    * */
    accLess: function (num1 = 0, num2 = 0, isPercent = false) {
        let arr1 = num1.toString().split(".");
        let arr2 = num2.toString().split(".");
        let newNum1 = "", newNum2 = "";
        if (arr1[1] || arr2[1]) {
            let length1 = !!arr1[1] ? arr1[1].length : 0;
            let length2 = !!arr2[1] ? arr2[1].length : 0;
            let maxLength = Math.max(length1, length2);
            newNum1 = parseInt(num1.toString().replace(".", ""));
            newNum2 = parseInt(num2.toString().replace(".", ""));
            let diff = Math.abs(length1 - length2), newStr = "";
            for (let i = 0; i < diff; i ++) {
                newStr += "0"
            }
            switch (true) {
                case length1 > length2:
                    newNum2 = newNum2 + "" + newStr;
                    break
                case length1 < length2:
                    newNum1 = newNum1 + "" + newStr;
                    break
                default:
                    break
            }

            let result = (newNum1 - newNum2) * (isPercent ? 100 : 1);
            let absResult = Math.abs(result);
            let resultString = absResult.toString();
            let integerNum = resultString.substr(0, resultString.length - maxLength);
            let floatNum = resultString.substr(resultString.length - maxLength);
            let newResult = integerNum.length > 0 ? (integerNum + "." + floatNum) : ("0." + floatNum);
            return (result < 0 ? "-" + newResult : newResult) - 0
        } else {
            return (num1 - num2) * (isPercent ? 100 : 1)
        }
    },

    /*
    * 保留小数点len位置
    * */
    numberToFixed: function (number = 0, len = 2) {
        let newNum = Math.abs(number);
        let power = Math.pow(10, len);
        newNum = Math.floor(newNum * power) / power;
        if (newNum.toString().indexOf(".") === -1) {
            newNum = newNum.toString() + ".00";
        } else if (newNum.toString().split(".")[1].length === 1) {
            newNum = newNum.toString() + "0";
        }

        return number >= 0 ? newNum : ("-" + newNum)
    }
}